package com.haoshuang.zhouzhoubang.ui.page.message.child.system

import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.FragmentSystemMessageBinding
import com.haoshuang.zhouzhoubang.model.bean.SystemMsgBean
import com.haoshuang.zhouzhoubang.ui.page.message.MessageFragment
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *
 */
class SystemMessageFragment :
    BaseVMFragment<SystemMessageViewModel, FragmentSystemMessageBinding>() {

    companion object {
        fun getInstance(): SystemMessageFragment {
            val fragment = SystemMessageFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    private val data = arrayListOf<SystemMsgBean>()
    private val adapter = SystemMessageAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {

        binding.apply {

            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                vm.getSystemMessage(p)
            }

            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                vm.getSystemMessage(p)
            }

            recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
            adapter.setOnItemClick { view, position, item ->
                if (item == null) return@setOnItemClick
                vm.masterSetMessageRead(item.id, 1)
            }


        }

    }

    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    override fun onResume() {
        super.onResume()
        binding.refreshLayout.autoRefresh()
    }

    override fun startObserve() {
        vm.getSystemMessageData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                val list = it?.data
                if (data.firstOrNull { it.isFirstRead } == null) {
                    val index = list?.indexOf(list.firstOrNull { it.is_read == 1 })
                    if (index != null && index != -1) {
                        list[index].isFirstRead = true
                    }
                }
                binding.refreshLayout.setupData(list, data, adapter, p)
                (parentFragment as MessageFragment).setUnReadButton(0, list?.firstOrNull { it.is_read == 0 } != null)
            }
        }

        vm.masterSetMessageReadData.observeState(this) {
            onSuccess { id ->
                val index = data.indexOf(data.firstOrNull { it.id == id })
                data[index].is_read = 1
                adapter.notifyItemChanged(index)
                (parentFragment as MessageFragment).refreshData()
            }
        }
    }

    fun setMessageRead() {
        data.forEach {
            it.is_read = 1
            it.isFirstRead = false
        }
        data.firstOrNull()?.isFirstRead = true
        adapter.notifyDataSetChanged()
    }

}